/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.odsKartW.web;

import cz.muni.fi.pb138.odsKartW.MediaManager;
import cz.muni.fi.pb138.odsKartW.MediaManagerImpl;
import cz.muni.fi.pb138.odsKartW.MediaSource;
import cz.muni.fi.pb138.odsKartW.Medium;
import cz.muni.fi.pb138.odsKartW.ODSKartWException;
import cz.muni.fi.pb138.odsKartW.Title;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class CreateMedium extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            doPost(request, response);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        MediaSource source = new MediaSource();
//        try {
//            source = new MediaSource();
//        } catch (URISyntaxException ex) {
//            Logger.getLogger(CreateMedium.class.getName()).log(Level.SEVERE, null, ex);
//        }
        MediaManager mediaManager = new MediaManagerImpl(new File(request.getSession().getServletContext().getRealPath(source.getSource())));
        String category = request.getParameter("categoryTitle");
        Medium medium = new Medium(category);
        String whatToDo = request.getParameter("submit_button");
        
        if (whatToDo.equals("Zruš")){
            ShowMedia showMedia = new ShowMedia();
            showMedia.setDataShow(request);
            request.setAttribute("categoryTitle", category);
            request.setAttribute("show", true);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
                
        List<String> header1 = new ArrayList<String>();
        try {
            header1 = mediaManager.getHeader(category);
        } catch (ODSKartWException ex) {
            Logger.getLogger(CreateMedium.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", ex.getMessage());
            request.getRequestDispatcher("create.jsp").forward(request, response);
        }
        
        for(String s : header1) {
            if(!s.equals("Id")) {
                medium.getTitles().add(new Title(request.getParameter(s)));
            }
        }
        try {
            mediaManager.createMedium(medium);
        } catch (ODSKartWException ex) {
            Logger.getLogger(CreateMedium.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", ex.getMessage());
            request.setAttribute("header1" , header1);
            request.setAttribute("categoryTitle", category);
            try{
            request.getRequestDispatcher("create.jsp").forward(request, response);
            } catch (Exception exc){
                request.setAttribute("error", exc.getMessage());
            }
        }
        
        ShowMedia showMedia = new ShowMedia();
        showMedia.setDataShow(request);
        
        request.setAttribute("addedItem", "Položka bola vytvorená");
        request.setAttribute("show", true);
      //  request.setAttribute("categoryTitle", category);
      //  request.setAttribute("show", true);
        try{
        request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (Exception e){
            request.setAttribute("error", e.getMessage());
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
